import { Component } from '@angular/core';
import { NavController, AlertController, ToastController } from 'ionic-angular';
import { AuthService } from '../../../../providers/auth-service';


@Component({
  selector: 'page-my-modify-pwd',
  templateUrl: 'pwd.html'
})

export class MyModifyPwdPage {
  user:any
  oldpwd = ''
  newpwd = ''
  repwd = ''
  constructor(public navCtrl: NavController,
   private auth: AuthService,
   private toast: ToastController,
   private alertCtrl: AlertController
   ) {
    this.user =  auth.getUserInfo().user
  }
  public modifyMyInfo() {
   if (this.user.password !== this.oldpwd) {
      this.toast.create({
        message: '您输入的旧密码有误',
        duration: 3000,
        position: 'middle'
      }).present();
      return
   }
   if (this.newpwd !== this.repwd) {
      this.toast.create({
        message: '两次密码不一致',
        duration: 3000,
        position: 'middle'
      }).present();
      return
   }
   this.user.password = this.newpwd
   this.auth.modifyUser(this.user).then(() => {
       this.showPopup("修改成功", "即将返回");
    })
  }
  showPopup(title, text) {
    let alert = this.alertCtrl.create({
      title: title,
      subTitle: text,
      buttons: [
       {
         text: 'OK',
         handler: data => {
           this.navCtrl.popToRoot();
         }
       }
     ]
    });
    alert.present();
  }
}
